<?php
require '../inc/basics.php';
hasPermission('accountManager','permissies');
?>
<script type="text/javascript" >
		$(".multiple").selectable({ filter:'li[id]'});

		//get assigned users in group member pane
		function assignedPermissions(id){
			$.getJSON("/a/f=adminAssignedPermissions", {id : id}, function(data){
				$("#select_user_assigned_permissions").empty();
				var plugin = '';
				var select = $("#select_user_assigned_permissions");
				$.each(data, function(key, val) {
					if(plugin != val.plugin) select.append("<li class='devider'>"+val.plugin+"</li>\n");
					select.append("<li id="+val.id+">"+val.name+"</li>\n");
					plugin = val.plugin;
				});
			});
		} 
		
		//get deassigned users in available users pane
		function deAssignedPermissions(id){
			$.getJSON("/a/f=adminDeassignedPermissions", {id : id}, function(data){
				$("#select_user_deassigned_permissions").empty();
				var plug = '';
				var select = $("#select_user_deassigned_permissions");
				$.each(data, function(key, val) {
					if(plug != val.plugin) select.append("<li class='devider'>"+val.plugin+"</li>\n");
					select.append("<li id="+val.id+">"+val.name+"</li>\n");
					plug = val.plugin;
				});
			});
			
		}
			
		$("#select_user_groups.selectable li").live('click', function() {
			$("#select_user_assigned_permissions").showLoading();
			$("#select_user_deassigned_permissions").showLoading();
			$(this).addClass("ui-selected").siblings().removeClass("ui-selected");
			$("ul .ui-selected").each(function () {
				assignedPermissions($(this).attr('id'));		
				deAssignedPermissions($(this).attr('id'));		
			});
			$("#select_user_assigned_permissions").hideLoading();
			$("#select_user_deassigned_permissions").hideLoading();
		});

		//grant permission(s) to a role
		$(":button#add").click(function() {
			var count = 0;
			$("#select_user_assigned_permissions").showLoading();
			$("#select_user_deassigned_permissions").showLoading();
			$("#select_user_deassigned_permissions .ui-selected").each(function () {
				count++;
				var id = $("#select_user_groups .ui-selected").attr('id');
				$.getJSON("/a/f=adminGrantPermission", {pid : $(this).attr('id'), rid : id }, function(data){
					if (data.succes == true){
						assignedPermissions(id);
						deAssignedPermissions(id);
						count--;
						if(count == 0){
							$("#select_user_assigned_permissions").hideLoading();
							$("#select_user_deassigned_permissions").hideLoading();							
						}
					}
				});
			});
			return false;
		});
		//revoke permission(s) to a role
		$(":button#remove").click(function() {
			var count = 0;
			$("#select_user_assigned_permissions").showLoading();
			$("#select_user_deassigned_permissions").showLoading();
			$("#select_user_assigned_permissions .ui-selected").each(function () {
				count++;
				var id = $("#select_user_groups .ui-selected").attr('id');
				$.getJSON("/a/f=adminRevokePermission", {pid : $(this).attr('id'), rid : id }, function(data){
					if (data.succes == true){
						deAssignedPermissions(id);						
						assignedPermissions(id);
						count--;
						if(count == 0){
							$("#select_user_assigned_permissions").hideLoading();
							$("#select_user_deassigned_permissions").hideLoading();							
						}
					}
				});
			});
			return false;
		});

</script>

<div class="inner-center">
	<form>
		<table class="user_accounts" >
			<tr><th colspan="2">Groups</th><th colspan="3">Available permissions</th><th>Assigned permissions</th></tr>
			<tr>
				<td colspan="1">
					<ul id="select_user_groups" class="selectable">
						<?php
						$administrator = Administrator::getInstance();
						$groups = $administrator->listRoles();
						foreach ($groups as $group) {
							echo "<li id=" . $group['id'] . ">" . htmlspecialchars($group['name']) . "</li>\n";
						}
						?>
					</ul>
				</td>
				<td width="30"></td>
				<td>
					<ul id="select_user_deassigned_permissions" class="selectable multiple" ></ul>
				</td>
				<td width="15"></td><td width="50"><br><br><br><br><br><br><button id="add">></button><br><br><br><button id="remove"><</button></td>
				<td>
					<ul id="select_user_assigned_permissions" class="selectable multiple"></ul>
				</td>
			</tr>
		</table>
	</form>
</div>